import axios from 'axios'
import { ResultType } from '@/types/result'

// const BASE_API = import.meta.env.VITE_BASE_API
// console.log('BASE_API', BASE_API)
// console.log('BASE_API', import.meta.env)

// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: '/api',
  // 超时
  timeout: 10000,
})

/**
 * 发送http请求
 * @param option 配置项
 * @returns 
 */
export function request(option: Record<string, unknown>): Promise<ResultType> {
  return new Promise<ResultType>((resolve, reject) => {
    service(option)
      .then((response) => {
        resolve(response.data)
      })
      .catch((error) => {
        console.error(error)
        reject(error)
      })
  })
}

/**
 * http get请求
 * @param url
 * @param params
 * @returns
 */
export function get(url: string, params?: Record<string, unknown>): Promise<ResultType> {
  return request({
    method: 'get',
    url,
    params
  })
}

/**
 * http get请求
 * @param url
 * @param params
 * @returns
 */
 export function post(url: string, params: Record<string, unknown>): Promise<ResultType> {
  return request({
    method: 'post',
    url,
    data: params,
  })
}

/**
 * http get请求
 * @param url
 * @param params
 * @returns
 */
export function patch(url: string, params: Record<string, unknown>): Promise<ResultType> {
  return request({
    method: 'patch',
    url,
    data: params,
  })
}

/**
 * http get请求
 * @param url
 * @param params
 * @returns
 */
export function $delete(url: string, params?: Record<string, unknown>): Promise<ResultType> {
  return request({
    method: 'delete',
    url,
    data: params,
  })
}